package demo.gaobo.com.myapplication.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;

import butterknife.Bind;
import demo.gaobo.com.myapplication.R;
import demo.gaobo.com.myapplication.base.BaseActivity;
import demo.gaobo.com.myapplication.base.BasePresenter;
import demo.gaobo.com.myapplication.fragment.ClassificationFragment;

public class ClassificationActivity extends BaseActivity {


    @Bind(R.id.tablayout)
    SlidingTabLayout tablayout;
    @Bind(R.id.view_pager)
    ViewPager viewPager;
    //设置适配器
    private MyPagerAdapter mAdapter;
    //Fragment集合
    private ArrayList<Fragment> mFagments = new ArrayList<>();
    //标题
    private String[] mTitles = {"头盔", "洗护用品", "保健品", "药品", "其他"};
    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_classification;
    }

    @Override
    protected String setToolBarTitle() {
        return "分类";
    }


    @Override
    public void initListener() {
        initTab();
    }


    private void initTab() {
        //添加Fragment
        mFagments.add(new ClassificationFragment());
        mFagments.add(new ClassificationFragment());
        mFagments.add(new ClassificationFragment());
        mFagments.add(new ClassificationFragment());
        mFagments.add(new ClassificationFragment());

        //new一个适配器
        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        //设置ViewPager与适配器关联
        viewPager.setAdapter(mAdapter);
        //设置Tab与ViewPager关联
        tablayout.setViewPager(viewPager);
    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFagments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFagments.get(position);
        }
    }
}
